var datagridDept;
var datagridExceptionType;
var dialogDept;
var dialogExceptionType;

$(function () {

    datagridDept = $('#mdg_dept').datagrid({
        method: "post",
        url: getRootPath() + '/exception/getDeptList',
        fit: true,
        fitColumns: true,
        border: true,
        idField: 'id',
        striped: true,
        pagination: false,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        toolbar: '#tb_dept',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: 'id', hidden: true},
            {
                field: 'dept_name', title: '责任部门名称', sortable: false, width: 40,
                formatter: function (value, row, index) {
                    return "<span title='" + value + "'>" + value + "</span>";
                }
            },
            {field: 'creat_date', title: '添加时间', sortable: false, width: 30},
            {field: 'remark', title: '备注', sortable: false, width: 30,
                formatter: function (value, row, index) {
                    return "<span title='" + value + "'>" + value + "</span>";
                }
            },
            {
                field: 'operate', title: '操作', width: 42,
                formatter: function (value, row, index) {
                    var d = "<a onclick='remove(" + row.id + ")' class='button-delete button-red'>删除</a>";
                    var e = "<a onclick='edit(" + index + ")' class='button-edit button-blue'>编辑</a>";
                    return e + '  ' + d;
                }
            }
        ]],
        onLoadSuccess:function(data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
        },
        queryParams: {

        }
    });
    IniExceptionType();
});

//初始化异常种类列表
function IniExceptionType() {
    datagridExceptionType = $('#mdg_exception_type').datagrid({
        method: "post",
        url: getRootPath() + '/exception/getExcTypeList',
        //fit: true,
        border: true,
        idField: 'id',
        striped: true,
        pagination: false,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        toolbar: '#tb_exceptionType',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: 'id', hidden: true},
            {field: 'exception_type_name', title: '异常名称', sortable: false, width: 120},
            {field: 'creat_date', title: '创建时间', sortable: false, width: 80},
            {
                field: 'operate', title: '操作', width: 120,
                formatter: function (value, row, index) {
                    var d = "<a onclick='remove_exceptionType(" + row.id + ")' class='button-delete button-red'>删除</a>";
                    var e = "<a onclick='edit_exceptionType(" + index + ")' class='button-edit button-blue'>编辑</a>";
                    return e + '  ' + d;
                }
            }
        ]],
        onLoadSuccess:function(data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
        },
        queryParams: {

        }
    });
}

function getFormJson(form, filterName, addName) {
    var o = {};
    var a = $(form).serializeArray();
    // addName是新增的键值对
    for (var i = 0; i < addName.length; i++) {
        a.push(addName[i]);
    }
    console.log(a);
    $.each(a, function () {
        console.log(this.name);
        if (o[this.name] !== undefined && this.name != filterName) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else if (this.name != filterName) {
            o[this.name] = this.value || '';
        }
    });
    console.log(o);
    return o;
}

/*function queryDept() {
    console.log($("#deptName").val());
    $(datagridDept).datagrid('load', {
            "dept_name": $("#DeptName").val()
        }
    );
}

function queryExceptionType() {
    $(datagridExceptionType).datagrid('load', {
            "exception_name": $("#exceptionTypeName").val()
        }
    );
}*/

/**
 * 添加责任部门
 */
function add() {

    dialogDept = $("#dlg_dept").dialog({
        title: '添加责任部门',
        width: 460,
        height: 300,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_dept").form('validate');

                var addObj = [];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/exception/addDept",
                        loading: true,
                        data: getFormJson("#form_dept", "", addObj),//$('#form_main').serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialogDept.dialog('close');
                                queryDept();
                            } else if (data.code == 400) {
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogDept.dialog('close');
            }
        }]
    });
    $("#form_dept").form("clear");
}

/**
 * 编辑责任部门
 */
function edit(id) {

    var row;
    if (id == null) {//点击datagrid上面的修改
        row = datagridDept.datagrid('getSelected');
    } else {//点击datagrid对应行上的修改
        datagridDept.datagrid('selectRow', id);
        row = datagridDept.datagrid('getSelected');
    }

    if (row == null) {
        U.msg('请先选择责任部门');
        return;
    }

    /*
    $.getJSON(getRootPath() + "/mtManage/assets/list_dept", function (json) {
        $('#deptInfo').combobox({
            data: json.data, //获取到的json 数据
            valueField: "dept_id",
            textField: "dept_name"
        });
        console.log(row.deptId);
        $("#deptInfo").combobox('setValue', row.deptId);
    });
    */

    dialogDept = $("#dlg_dept").dialog({
        title: '编辑责任部门',
        width: 460,
        height: 300,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_dept").form('validate');
                var addObj = [{"name":"id","value":row.id}];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/exception/editDept",
                        loading: true,
                        data: getFormJson("#form_dept", "", addObj),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialogDept.dialog('close');
                                queryDept();
                            }else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            }else if (data.code == 403) {
                                U.msg('非法请求');
                            }else if (data.code == 404) {
                                U.msg('未找到该分类');
                            } else if (data.code == 409) {
                                U.msg('该分类已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogDept.dialog('close');
            }
        }]
    });
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#dept_name", row.dept_name);
    setVal("#remark", row.remark);
}

/**
 * 删除责任部门
 */
function remove(id) {
    if (id == null) {
        var row = datagridDept.datagrid('getSelected');
        if (row == null) {
            U.msg('请先选择责任部门');
            return;
        } else {
            id = row.id;
        }
    }

    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
        if (data) {
            U.get({
                url: getRootPath() + '/exception/deleDept?id=' + id,
                loading: true,
                success: function (data) {
                    if (data.code == 200) {
                        U.msg('删除成功');
                        queryDept();
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 403) {
                        U.msg('非法访问');
                    }
                    else {
                        U.msg('服务器异常');
                    }
                }
            });
        }
    });
}

/**
 * 添加异常种类
 */
function add_exceptionType() {

    dialogExceptionType = $("#dlg_exception").dialog({
        title: '添加分类',
        width: 460,
        height: 150,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_exception").form('validate');
                var addObj = [];
                var obj = getFormJson("#form_exception", "", addObj);
                obj.operator = username;
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/exception/addExcType",
                        loading: true,
                        data: obj,//自定义过滤字段和追加字段
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialogExceptionType.dialog('close');
                                queryExceptionType();
                            } else if (data.code == 400) {
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('异常种类已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogExceptionType.dialog('close');
            }
        }]
    });
    $("#form_exception").form('clear');
}

/**
 * 编辑异常种类
 */
function edit_exceptionType(id) {

    var row;
    if (id == null) {//点击datagrid上面的修改
        row = datagridExceptionType.datagrid('getSelected');
    } else {//点击datagrid对应行上的修改
        datagridExceptionType.datagrid('selectRow', id);
        row = datagridExceptionType.datagrid('getSelected');
    }

    if (row == null) {
        U.msg('请先选择异常类型');
        return;
    }

    dialogExceptionType = $("#dlg_exception").dialog({
        title: '编辑异常种类',
        width: 460,
        height: 150,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_exception").form('validate');
                var addObj = [{"name": "id", "value": row.id}];
                var obj = getFormJson("#form_exception", "", addObj);
                obj.operator = username;
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/exception/editExcType",
                        loading: true,
                        data: obj,
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialogExceptionType.dialog('close');
                                queryExceptionType();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 404) {
                                U.msg('未找到该分类');
                            } else if (data.code == 409) {
                                U.msg('该分类已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogExceptionType.dialog('close');
            }
        }]
    });
    $("#form_exception").form('clear');
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#exception_name", row.exception_type_name);
}

/**
 * 删除异常类别
 */
function remove_exceptionType(id) {
    if (id == null) {
        var row = datagridExceptionType.datagrid('getSelected');
        if (row == null) {
            U.msg('请先选择物资');
            return;
        } else {
            id = row.id;
        }
    }

    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
        if (data) {
            U.get({
                url: getRootPath() + '/exception/deleExcType?id=' + id,
                loading: true,
                success: function (data) {
                    if (data.code == 200) {
                        U.msg('删除成功');
                        queryExceptionType();
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 404) {
                        U.msg('未找到该分类');
                    } else if (data.code == 403) {
                        U.msg('非法访问');
                    }else if (data.code == 424) {
                        U.msg('该异常类别已被使用，无法删除');
                    } else {
                        U.msg('服务器异常');
                    }
                }
            });
        }
    });
}

function queryExceptionType() {
    console.log($("#exceptionTypeName").val());
    $('#mdg_exception_type').datagrid('load', {
            "exception_type_name": $("#exceptionTypeName").val()
        }
    );
}

function queryDept() {
    $('#mdg_dept').datagrid('load', {
            "dept_name": $("#deptName").val()
        }
    );
}
